package xyz.chaobei.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.chaobei.entity.OrgRoleEntity;
import xyz.chaobei.execption.NullDataException;
import xyz.chaobei.service.OrgRoleService;
import xyz.chaobei.util.JsonUtil;

import java.util.List;

/**
 * 前端控制器
 *
 * @author mrc
 */
@RestController
@RequestMapping("api/orgRole")
public class OrgRoleController {

    @Autowired
    private OrgRoleService orgRoleService;

    @PostMapping("/page")
    public JSONObject page(OrgRoleEntity orgRoleEntity) {

        Page<OrgRoleEntity> page = new Page<>();
        orgRoleService.page(page);

        return JsonUtil.getSuccessPage(page);
    }

    @GetMapping("/{id}")
    public JSONObject getRole(@PathVariable("id") Integer id) {

        OrgRoleEntity entity = orgRoleService.getById(id);
        return JsonUtil.getSuccess(entity);
    }

    @GetMapping("/")
    public JSONObject list() {
        List<OrgRoleEntity> list = orgRoleService.list();
        return JsonUtil.getSuccess(list);
    }

    @PostMapping("/")
    public JSONObject update(@RequestBody OrgRoleEntity entity) {
        boolean isUpdate = orgRoleService.save(entity);
        return JsonUtil.getSuccess(isUpdate);
    }

    @PutMapping("/{id}")
    public JSONObject update(@RequestBody OrgRoleEntity entity, @PathVariable("id") Integer id) {

        OrgRoleEntity dbEntity = orgRoleService.getById(id);

        if (null == dbEntity) {
            throw new NullDataException();
        }
        entity.setId(id);
        boolean isUpdate = orgRoleService.updateById(entity);

        return JsonUtil.getSuccess(isUpdate);
    }

    /**
     * 假删除数据
     *
     * @return
     */
    @DeleteMapping("/{id}")
    public JSONObject delete(@PathVariable("id") Integer id) {
        boolean isUpdate = orgRoleService.removeById(id);
        return JsonUtil.getSuccess(isUpdate);
    }


}
